home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / clocks.zip / CLOCKS.PAS < prev    next >
Pascal/Delphi Source File  |  1990-12-17  |  6KB  |  280 lines

  1. unit clocks;
  2. {$X+}  {allow discardable function results}
  3.  
  4. { Clock-on-a-menubar OOP extension to Turbo Vision apps
  5.  
  6.   Copyright (c) 1990 by Danny Thorpe
  7.  
  8.   Alarms have not been implemented.
  9. }
  10.  
  11. interface
  12. uses dos, objects, drivers, views, menus, dialogs, app, msgbox;
  13.  
  14. const  cmClockChangeDisplay = 1001;
  15.        cmClockSetAlarm = 1002;
  16.  
  17.        ClockNoSecs   = 0;
  18.        ClockDispSecs = 1;
  19.        Clock12hour   = 0;
  20.        Clock24hour   = 1;
  21.  
  22. type
  23.  
  24.      ClockDataRec = record
  25.        Format: word;
  26.        Seconds: word;
  27.        RefreshStr: String[2];
  28.        end;
  29.  
  30.  
  31.      PClockMenu = ^TClockMenu;
  32.      TClockMenu = object(TMenuBar)
  33.        ClockOptions: ClockDataRec;
  34.        Refresh: byte;
  35.        LastTime: DateTime;
  36.        TimeStr: string[10];
  37.        constructor Init(var Bounds: TRect; Amenu: PMenu);
  38.        procedure Draw;   virtual;
  39.        procedure Update; virtual;
  40.        procedure SetRefresh(Secs: integer);        virtual;
  41.        procedure SetRefreshStr( Secs: string);     virtual;
  42.        procedure ClockChangeDisplay;               virtual;
  43.        procedure HandleEvent( var Event: TEvent);  virtual;
  44.        function  FormatTimeStr(h,m,s:word):string; virtual;
  45.        end;
  46.  
  47.  
  48.  
  49.  
  50. implementation
  51.  
  52.  
  53. function LeadingZero(w : Word) : String;
  54. var
  55.   s : String;
  56. begin
  57.   Str(w:0,s);
  58.   if Length(s) = 1 then
  59.     s := '0' + s;
  60.   LeadingZero := s;
  61. end;
  62.  
  63.  
  64.  
  65. constructor TClockMenu.Init(var Bounds: TRect; AMenu: PMenu);
  66.   var Temp: PMenuBar;
  67.       ClockMenu: PMenu;
  68.       R: TRect;
  69.   begin
  70.   ClockMenu:= NewMenu(NewSubMenu('~'#0'~Clock ', hcNoContext, NewMenu(
  71.                 NewItem('~C~hange display','',0,cmClockChangeDisplay, hcNoContext,
  72.                 NewItem('Set ~A~larm','', 0, cmClockSetAlarm, hcNoContext,
  73.                 nil))),
  74.                 AMenu^.Items));
  75.                 { ^^ tack passed menubar on end of new clock menu }
  76.   ClockMenu^.Default:= AMenu^.Default;
  77.  
  78.   TMenuBar.Init(Bounds, ClockMenu);
  79.  
  80.   fillchar(LastTime,sizeof(LastTime),#$FF);   {fill with 65000's}
  81.   TimeStr:='';
  82.   ClockOptions.Format:= Clock24Hour;
  83.   ClockOptions.Seconds:= ClockDispSecs;
  84.   SetRefresh(1);
  85.   end;
  86.  
  87.  
  88.  
  89. procedure TClockMenu.Draw;
  90.   var P: PMenuItem;
  91.   begin
  92.   P:= FindItem(#0);
  93.   if P <> nil then
  94.     begin
  95.     DisposeStr(P^.Name);
  96.     P^.Name:= NewStr('~'#0'~'+TimeStr);
  97.     end;
  98.   TMenuBar.Draw;
  99.   end;
  100.  
  101.  
  102.  
  103. procedure TClockMenu.Update;
  104.   var h,m,s,hund: word;
  105.   begin
  106.     GetTime(h,m,s,hund);
  107.     if abs(s-LastTime.sec) >= Refresh then
  108.       begin
  109.       with LastTime do
  110.         begin
  111.         Hour:=h;
  112.         Min:=m;
  113.         Sec:=s;
  114.         end;
  115.       TimeStr:= FormatTimeStr(h,m,s);
  116.       DrawView;
  117.       end;
  118.   end;
  119.  
  120.  
  121.  
  122.  
  123. procedure TClockMenu.SetRefresh(Secs: integer);
  124.   begin
  125.   if Secs > 59 then
  126.     Secs := 59;
  127.   if Secs < 0 then
  128.     Secs := 0;
  129.   Refresh:= Secs;
  130.   Str(Refresh:2,ClockOptions.RefreshStr);
  131.   end;
  132.  
  133.  
  134.  
  135. procedure TClockMenu.SetRefreshStr( Secs: string);
  136.   var temp,code: integer;
  137.   begin
  138.   val(Secs, temp, code);
  139.   if code = 0 then
  140.     SetRefresh(temp);
  141.   end;
  142.  
  143.  
  144.  
  145.  
  146. procedure TClockMenu.ClockChangeDisplay;
  147.  
  148.   var
  149.     D: PDialog;
  150.     Control: PView;
  151.     Command: word;
  152.     temp,code: integer;
  153.     R: TRect;
  154.     ClockData : ClockDataRec;
  155.  
  156.   begin
  157.  
  158.   ClockData := ClockOptions;
  159.  
  160.   R.Assign(14,3,48,15);
  161.   D:= new(PDialog, Init(R, 'Clock Display'));
  162.  
  163.   R.Assign(3,3,20,5);
  164.   Control:= new(PRadioButtons, Init(R,
  165.             NewSItem('~1~2 hour',
  166.             NewSItem('~2~4 hour',
  167.             nil))));
  168.   D^.Insert(Control);
  169.  
  170.   R.Assign(3,2,20,3);
  171.   Control:= new(Plabel, Init(R, '~F~ormat', Control));
  172.   D^.Insert(Control);
  173.  
  174.   R.Assign(3,6,20,7);
  175.   Control:= new(PCheckBoxes, Init(R,
  176.             NewSItem('~S~econds',
  177.             nil)));
  178.   D^.Insert(Control);
  179.  
  180.   R.Assign(16,9,20,10);
  181.   Control:= new(PInputLine, Init(R, 2));
  182.   D^.Insert(Control);
  183.  
  184.   R.Assign(2,8,20,9);
  185.   Control:= new(PLabel, Init(R, '~R~efresh Rate', Control));
  186.   D^.Insert(Control);
  187.  
  188.   R.Assign(2,9,15,10);
  189.   Control:= new(PLabel, Init(R, '0-59 seconds', PLabel(Control)^.Link));
  190.   D^.Insert(Control);
  191.  
  192.   R.Assign(21,3,31,5);
  193.   Control:= new(PButton, Init(R, '~O~k', cmOk, bfDefault));
  194.   D^.Insert(Control);
  195.  
  196.   R.Assign(21,6,31,8);
  197.   Control:= new(PButton, Init(R, '~C~ancel', cmCancel, bfNormal));
  198.   D^.Insert(Control);
  199.  
  200.  
  201.   D^.SelectNext(False);
  202.   D^.SetData(ClockData);
  203.   repeat
  204.     Command:= Desktop^.ExecView(D);
  205.     if Command = cmOK then
  206.       begin
  207.       D^.GetData(ClockData);
  208.       val(ClockData.RefreshStr,temp,code);
  209.       if (code <> 0) or ((temp<0) or (temp>59)) then
  210.         MessageBox('Refresh rate must be between 0 and 59 seconds.',nil,
  211.            mfOKButton+mfError);
  212.       end;
  213.   until (Command = cmCancel)
  214.      or ((code=0) and ((temp>=0) and (temp<=59)));
  215.  
  216.   Dispose(D, Done);
  217.  
  218.   if Command = cmOk then
  219.     begin
  220.     ClockOptions:= ClockData;
  221.     SetRefreshStr(ClockData.RefreshStr);
  222.     end;
  223.  
  224.   { update display to reflect changes immediately }
  225.   TimeStr:= FormatTimeStr(LastTime.hour, LastTime.min, LastTime.sec);
  226.   DrawView;
  227.   end;
  228.  
  229.  
  230.  
  231.  
  232.  
  233. procedure TClockMenu.HandleEvent( var Event: TEvent);
  234.   begin
  235.   TMenuBar.HandleEvent( Event);
  236.   if Event.What = evCommand then
  237.     begin
  238.     case Event.Command of
  239.       cmClockChangeDisplay: ClockChangeDisplay;
  240.       cmClockSetAlarm: ;
  241.       end;
  242.     end;
  243.   end;
  244.  
  245.  
  246.  
  247.  
  248. function TClockMenu.FormatTimeStr(h,m,s: word): string;
  249.   var st, tail: string;
  250.   begin
  251.   tail:='';
  252.   if ClockOptions.Format = Clock24Hour then
  253.     st:= LeadingZero(h)
  254.   else
  255.     begin
  256.     if h >= 12 then
  257.       begin
  258.       tail:= 'pm';
  259.       if h>12 then
  260.         dec(h,12);
  261.       end
  262.     else
  263.       tail:= 'am';
  264.     if h=0 then h:=12;   {12 am}
  265.     str(h:0,st);    { no leading space on hours }
  266.     end;
  267.  
  268.   st:=st+':'+ LeadingZero(m);
  269.  
  270.  
  271.   if ClockOptions.Seconds = ClockDispSecs then
  272.     st:= st+':'+LeadingZero(s);
  273.  
  274.   FormatTimeStr:= st + tail;
  275.   end;
  276.  
  277.  
  278.  
  279.  
  280. end.